/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package elaborate.tool.messenger.yahoo;

import com.fasterxml.jackson.databind.ObjectMapper;
import elaborate.tool.messenger.MessageListener;
import elaborate.tool.messenger.StringMessage;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

/**
 *
 * @author lendle
 */
public class MessageReceiverThread extends Thread {

    private String oauth_consumer_key;
    private String oauth_token;
    private String consumer_secret;
    private String oauthSecret;
    private String sessionId;
    private boolean running = true;
    private List<MessageListener> listeners = new ArrayList<MessageListener>();
    private int lastSeq=-1;
    private String userId=null;
    private HttpUtil httpUtil=null;

    public String getOauth_consumer_key() {
        return oauth_consumer_key;
    }

    public void setOauth_consumer_key(String oauth_consumer_key) {
        this.oauth_consumer_key = oauth_consumer_key;
    }

    public String getOauth_token() {
        return oauth_token;
    }

    public void setOauth_token(String oauth_token) {
        this.oauth_token = oauth_token;
    }

    public String getConsumer_secret() {
        return consumer_secret;
    }

    public void setConsumer_secret(String consumer_secret) {
        this.consumer_secret = consumer_secret;
    }

    public String getOauthSecret() {
        return oauthSecret;
    }

    public void setOauthSecret(String oauthSecret) {
        this.oauthSecret = oauthSecret;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public void addMessageListener(MessageListener l) {
        if (!this.listeners.contains(l)) {
            this.listeners.add(l);
        }
    }

    public MessageReceiverThread(String oauth_consumer_key, String consumer_secret, String oauth_token, String oauthSecret, String sessionId, String userId) {
        this.oauth_consumer_key = oauth_consumer_key;
        this.oauth_token = oauth_token;
        this.consumer_secret = consumer_secret;
        this.oauthSecret = oauthSecret;
        this.sessionId = sessionId;
        this.userId=userId;
        //initialize httputil
        this.httpUtil=new HttpUtil(oauth_consumer_key, consumer_secret, oauth_token, oauthSecret, sessionId, userId);
        this.setDaemon(true);
    }

    

    public void run() {
        //double oauth_nonce=24829.2330;
        while (running) {
            try {
                Thread.sleep(5000);
                String ret=httpUtil.doGet("http://developer.messenger.yahooapis.com/v1/notifications" + "?sid=" + sessionId+"&seq="+(lastSeq+1));
                //System.out.println(ret);
                ObjectMapper mapper = new ObjectMapper();
                Map map = mapper.readValue(ret, Map.class);
                List responses=(List) map.get("responses");
                for(int i=0; responses!=null && i<responses.size(); i++){
                    Map message=(Map) responses.get(i);
                    if(message.containsKey("message")){
                        message=(Map) message.get("message");
                        //System.out.println(message.get("msg"));
                        lastSeq=Integer.valueOf(""+message.get("sequence"));
                        for(MessageListener l : this.listeners){
                            l.onMessageReceived((String)message.get("sender"), (String)message.get("receiver"), new StringMessage( (String) message.get("msg") ));
                        }
                    }
                }
                
                //System.out.println("process message: " + message);
            } catch (Exception e) {
                e.printStackTrace();
                Logger.getLogger(this.getClass().getName()).severe(e.getMessage());
            }
        }
    }

    public void removeMessageListener(MessageListener l) {
        if (this.listeners.contains(l)) {
            this.listeners.remove(l);
        }
    }

    public void shutdown() {
        this.running = false;
    }
}
